package plgutils

import (
	"dgo/MyProj/PrjHttpUtils"
	"dgo/goutils/utils"
	"fmt"
	"time"
)

type debugstepsession struct {
	id         string
	requeststr string
	startt     time.Time
	debugstep  string
}

func (this *ObjStoragePlg) checkGetDebugStepSession(sessid string, newflag bool) *debugstepsession {
	if !newflag {
		obj := this.tasklst.Get(sessid, (*debugstepsession)(nil))
		if obj == nil {
			return nil
		}
		return obj.(*debugstepsession)
	} else {
		rval := this.tasklst.LoadOrStoreFunc(sessid, func() interface{} {
			rval := &debugstepsession{id: sessid}
			return rval
		}, nil).(*debugstepsession)
		return rval
	}
}

/*
*
获取对象:

	{
	   "sessionid":"", "id":"", "match":"", "requestdata":""
	}

"code", 0, "id":"", "data":{}, "msg"
* 响应
errcode:

	101: 参数不完整
	102: 验证失败
*/
func (this *ObjStoragePlg) onHttpGet(req *PrjHttpUtils.HttpRec) {
	taskid := fmt.Sprintf("%s-%d", utils.NowIDString(), utils.NewObjectDNA64())
	task := this.checkGetDebugStepSession(taskid, true)
	task.startt = time.Now()
	defer func() {
		this.tasklst.Remove(taskid)
	}()

	sessionid := req.QryAsString("sessionid", "")
	if len(sessionid) == 0 {
		req.ResponseJSONValues("code", -1, "errcode", 101, "msg", "缺少sessionid")
		return
	}

	json := req.Req
	json.SetKeyValue("sessionid", sessionid)
	json.SetKeyValue("id", req.QryAsString("id", ""))
	json.SetKeyValue("match", req.QryAsString("match", ""))
	task.requeststr = json.AsJSONStringEx(false, false)
	rval := this.GetValue(1, json, task)
	if err, ok := rval.(error); ok {
		this.LogWarnMessage("获取失败:%s, req:%s", err.Error(), json.String())
		req.ResponseJSONValues("code", -1, "errcode", 102, "msg", err.Error())
		return
	}
	json.SetKeyValue("code", 0)
	req.ResponseAJSON(json)
	return
}

/*
*
* 提交参数
{"sessionid":"", "id":"XXX", "data":""}

* 响应
errcode:

	102: 心跳失败
*/
func (this *ObjStoragePlg) onHttpPing(req *PrjHttpUtils.HttpRec) {
	sessionid := req.QryAsString("sessionid", "")
	if len(sessionid) == 0 {
		req.ResponseJSONValues("code", -1, "errcode", 101, "msg", "缺少sessionid")
		return
	}

	objectid := req.QryAsString("id", "")
	if len(objectid) == 0 {
		req.ResponseJSONValues("code", -1, "errcode", 101, "msg", "缺少对象id")
		return
	}

	json := req.Req
	json.SetKeyValue("sessionid", sessionid)
	json.SetKeyValue("id", objectid)
	err := this.OperaAction(1, json)
	if err != nil {
		req.ResponseJSONValues("code", -1, "errcode", 102, "msg", err.Error())
		return
	}
	req.ResponseJSONValues("code", 0)
	return
}

/*
*

	state:1 断线
*/
func (this *ObjStoragePlg) onHttpRelease(req *PrjHttpUtils.HttpRec) {
	sessionid := req.QryAsString("sessionid", "")
	if len(sessionid) == 0 {
		req.ResponseJSONValues("code", -1, "errcode", 101, "msg", "缺少sessionid")
		return
	}

	objectid := req.QryAsString("id", "")
	if len(objectid) == 0 {
		req.ResponseJSONValues("code", -1, "errcode", 101, "msg", "缺少对象id")
		return
	}

	json := req.Req
	json.SetKeyValue("sessionid", sessionid)
	json.SetKeyValue("id", objectid)
	json.SetKeyValue("reason", req.QryAsString("reason", ""))
	err := this.OperaAction(2, json)
	if err != nil {
		req.ResponseJSONValues("code", -1, "msg", err.Error())
		return
	}
	req.ResponseJSONValues("code", 0)
	return
}
